/*
 * @Author: your name
 * @Date: 2020-07-14 22:44:46
 * @LastEditTime: 2021-08-01 16:27:50
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \MWZ_MISc:\js\my_report\src\lib\ClientInformation.js
 */

// 判断是否是360浏览器
function _mime (option, value) {
  const mimeTypes = navigator.mimeTypes;
  for (const mt in mimeTypes) {
    if (mimeTypes[mt][option] === value) {
      return true;
    }
  }
  return false;
}

// 获得浏览器版本
function getBrowserInfo () {
  // const Sys = {};

  const ua = navigator.userAgent.toLowerCase();
  // var re =/(msie|firefox|chrome|opera|micromessenger|version).*?([\d.]+)/;
  /* let re = /(micromessenger).*?([\d.]+)/;
  let m = ua.match(re);
  if (!m) {
    re = /(msie|firefox|chrome|opera|version).*?([\d.]+)/;
    m = ua.match(re);
    Sys.browser = m[1].replace(/version/, 'safari');
  }
  Sys.browser = m[1];
  Sys.ver = m[2];

  return Sys */

  let browserType = null;
  let browserVer = '';
  const re = /(msie|firefox|chrome|opera|netscape|version).*?([\d.]+)/i;
  const m = ua.match(re);
  if (m && m[2]) {
    browserVer = m[2];
  }

  if (/micromessenger/i.test(ua)) {
    browserType = 'micromessenger'; // '微信浏览器'
    try {
      browserVer = ua.match(/micromessenger\/([\d.]+)/i)[1]
    } catch (e) {
      browserVer = '';
    }
  } else if (/dingtalk/i.test(ua)) {
    browserType = 'dingtalk'; // '钉钉'
    try {
      browserVer = ua.match(/dingtalk\/([\d.]+)/i)[1];
    } catch (e) {
      browserVer = '';
    }
  } else if (/alipayclient/i.test(ua)) {
    browserType = 'alipayclient'; // '支付宝'
    try {
      browserVer = ua.match(/alipayclient\/([\d.]+)/i)[1];
    } catch (e) {
      browserVer = '';
    }
  } else if (ua.match(/firefox/) !== null) {
    browserType = 'firefox'; // '火狐'
  } else if (ua.match(/ubrowser/) !== null) {
    browserType = 'ubrowser'; // 'UC'
    try {
      browserVer = ua.match(/ubrowser\/([\d.]+)/i)[1];
    } catch (e) {
      browserVer = '';
    }
  } else if (ua.match(/opera/) !== null) {
    browserType = 'opera'; // '欧朋'
  } else if (ua.match(/bidubrowser/) !== null) {
    browserType = 'bidubrowser';// '百度'
    try {
      browserVer = ua.match(/bidubrowser\/([\d.]+)/i)[1];
    } catch (e) {
      browserVer = '';
    }
  } else if (ua.match(/metasr/) !== null || ua.match(/se 2.x/) !== null) {
    browserType = 'metasr'; // '搜狗'
    browserVer = '';
  } else if (ua.match(/tencenttraveler/) !== null || ua.match(/qqbrowser/) !== null) {
    browserType = 'tencenttraveler'; // 'QQ'
    try {
      browserVer = ua.match(/tencenttraveler\/([\d.]+)/i) !== null ? ua.match(/tencenttraveler\/([\d.]+)/i)[1] : (ua.match(/qqbrowser\/([\d.]+)/i) !== null ? ua.match(/qqbrowser\/([\d.]+)/i)[1] : '');
    } catch (e) {
      browserVer = '';
    }
  } else if (ua.match(/maxthon/) !== null) {
    browserType = 'maxthon'; // '遨游'
    try {
      browserVer = ua.match(/maxthon\/([\d.]+)/i)[1];
    } catch (e) {
      browserVer = '';
    }
  } else if (ua.match(/lbbrowser/) !== null) {
    browserType = 'lbbrowser'; // '猎豹'
    try {
      browserVer = ua.match(/lbbrowser\/([\d.]+)/i)[1];
    } catch (e) {
      browserVer = '';
    }
  } else if (ua.match(/oppobrowser/i) !== null) {
    browserType = 'oppobrowser'; // 'oppo 浏览器'
    try {
      browserVer = ua.match(/oppobrowser\/([\d.]+)/i)[1];
    } catch (e) {
      browserVer = '';
    }
  } else if (ua.match(/XiaoMi\/MiuiBrowser/i) !== null) {
    browserType = 'XiaoMi/MiuiBrowser'; // '小米 浏览器'
    try {
      browserVer = ua.match(/MiuiBrowser\/([\d.]+)/i)[1];
    } catch (e) {
      browserVer = '';
    }
  } else if (ua.match(/FxiOS/i) !== null) {
    browserType = 'FxiOS'; // '火狐'
    try {
      browserVer = ua.match(/FxiOS\/([\d.]+)/i)[1];
    } catch (e) {
      browserVer = '';
    }
  } else if (ua.match(/OPR/i) !== null) {
    browserType = 'OPR'; // 'Opera'
    try {
      browserVer = ua.match(/OPR\/([\d.]+)/i)[1];
    } catch (e) {
      browserVer = '';
    }
  } else if (ua.match(/chrome/) !== null) {
    const is360 = _mime('type', 'application/vnd.chromium.remoting-viewer')
    if (is360) {
      browserType = '360'
      browserVer = ''
    } else {
      browserType = 'chrome'
    }
  } else if (!!window.ActiveXObject || 'ActiveXObject' in window) {
    browserType = 'IE';
    if (!ua.match(/msie/) && ua.match(/trident/) !== null) {
      browserVer = '11+';
    }
  } else if (ua.match(/safari/) !== null) {
    browserType = 'Safari';
    browserVer = ua.match(/safari\/([\d.]+)/i)[1];
  } else {
    browserType = 'other'; // '其他';
    browserVer = '';
  }

  return {
    browser: browserType,
    ver: browserVer
  };
}

// 获得操作系统
function getOSInfo () {
  let version = '';
  const userAgent = navigator.userAgent.toLowerCase();
  if (userAgent.indexOf('win') > -1) {
    if (userAgent.indexOf('windows nt 5.0') > -1 || userAgent.indexOf('Windows 2000') > -1) {
      version = 'Windows 2000';
    } else if (userAgent.indexOf('windows nt 5.1') > -1 || userAgent.indexOf('Windows XP') > -1) {
      version = 'Windows XP';
    } else if (userAgent.indexOf('windows nt 5.2') > -1 || userAgent.indexOf('Windows 2003') > -1) {
      version = 'Windows 2003';
    } else if (userAgent.indexOf('windows nt 6.0') > -1 || userAgent.indexOf('Windows Vista') > -1) {
      version = 'Windows Vista';
    } else if (userAgent.indexOf('windows nt 6.1') > -1 || userAgent.indexOf('windows 7') > -1) {
      version = 'Windows 7';
    } else if (userAgent.indexOf('windows nt 6.2') > -1 || userAgent.indexOf('windows 8') > -1) {
      version = 'Windows 8';
    } else if (userAgent.indexOf('windows nt 6.3') > -1) {
      version = 'Windows 8.1';
    } else if (userAgent.indexOf('windows nt 6.4') > -1 || userAgent.indexOf('windows nt 10') > -1) {
      version = 'Windows 10';
    } else {
      version = 'Unknown';
    }
  } else if (userAgent.indexOf('iphone') > -1) {
    version = 'Iphone';
  } else if (userAgent.indexOf('mac') > -1) {
    version = 'Mac';
  } else if (userAgent.indexOf('x11') > -1 || userAgent.indexOf('unix') > -1 || userAgent.indexOf('sunname') > -1 || userAgent.indexOf('bsd') > -1) {
    version = 'Unix';
  } else if (userAgent.indexOf('linux') > -1) {
    if (userAgent.indexOf('android') > -1) {
      version = 'Android'
    } else {
      version = 'Linux';
    }
  } else {
    version = 'Unknown';
  }
  return version;
}

export const browserInfo = getBrowserInfo;
export const osInfo = getOSInfo;
